<?php

/**
 * 花集通京东订单API(为了方便视图呈现，重新调整数据层级等)
 *
 * @author cjj
 */
class OrderBCAPI {

    private $_client;

    /**
     * Constructor
     */
    public function __construct($options = array()) {
        $client = isset($options[0]) ? $options[0] : null;
        if (!$client || !$client instanceof HJClient) {
            throw new Exception(30017);
        }
        $this->_client = $client;
    }

    /**
     * 搜索订单列表
     *
     * @param int $page
     * @param int $page_size
     * @param array $options
     *
     * @return mixed
     */
    public function search($options = array()) {
        
        return $this->_client->api('/orders/sapi/', array(
                'min_modifyed' => $options['min_modifyed'],
                'max_modifyed' => $options['max_modifyed'],
                'status' => $options['status'],
                'page' => $options['page'],
        ));
        
    }

    /**
     * 查询备注和等级(根据订单id)
     *
     * @param $order_id
     *
     * @return array|null
     * @throws APIException
     */
    public function queryVenderRemarkByOrderId($order_id) {
        if (!$order_id) {
            return null;
        }
        $paras = array(
            'order_id' => $order_id
        );
        return $this->_client->api('jingdong.order.venderRemark.queryByOrderId', $paras);
    }

    /**
     * 更新备注
     *
     * @param $tid
     * @param $memo
     * @param $flag
     *
     * @return mixed
     */
    public function updateMemo($tid, $memo = ' ', $flag = null) {
        if (!$tid) {
            return null;
        }
        $paras = array(
            'remark' => $memo,
            'order_id' => $tid
        );
        if ($flag !== null && $flag >= 0) {
            $paras['flag'] = $flag;
        }
        return $this->_client->api('360buy.order.vender.remark.update', $paras);
    }

    /**
     * SOP商户 出库
     *
     * @param string $tid 订单id
     * @param int logistics_id 物流公司id
     * @param string $waybill 运单号(如果是自送，可为空)
     *
     * @return mixed
     */
    public function sopOutStorage($tid, $logistics_id, $waybill = '') {
        //必填参数
        $paras = array(
            'order_id' => $tid,
            'logistics_id' => $logistics_id
        );
        //可选参数
        if ($waybill) {
            $paras['waybill'] = $waybill;
        }
        return $this->_client->api('360buy.order.sop.outstorage', $paras);
    }

    /**
     * 根据京东ORDERID 获取交易数据
     *
     * @param $orderId
     *
     * @return mixed
     */
    public function getOrderByTid($orderId) {
        return $this->_client->api("/orders/sapi/{$orderId}/", array());
    }

    /**
     * 订单备注改动数据
     *
     * @param $startTime
     * @param $endTime
     * @param $page
     * @return mixed
     */
    public function getRemarkByModifyTime($startTime, $endTime, $page) {
        return $this->_client->api('jingdong.pop.order.getRemarkByModifyTime', array(
                    'startTime' => $startTime,
                    'endTime' => $endTime,
                    'page' => $page,
        ));
    }

     /**
     * 订单新增备注数据
     *
     * @param $startTime
     * @param $endTime
     * @param $page
     * @return mixed
     */
    public function getRemarkByCreateTime($startTime, $endTime, $page) {
        return $this->_client->api('jingdong.pop.order.getRemarkByCreateTime', array(
                    'startTime' => $startTime,
                    'endTime' => $endTime,
                    'page' => $page,
        ));
    }
    
    /**
     * 获取sku信息
     *
     * @param $sku_id
     * @return mixed
     */
    public function getSkuByID($sku_id) {
        return $this->_client->api('360buy.ware.sku.get', array(
            'sku_id' => $sku_id,
        ));
    }

    /**
     * 获取sku信息
     *
     * @param $sku_id
     * @return mixed
     */
    public function getWareByID($ware_id, $attribute_value_id) {
        return $this->_client->api('360buy.ware.propimgs.get', array(
            'ware_id' => $ware_id,
            'attribute_value_id' => $attribute_value_id
        ));
    }

    /**
     * 获取商品信息
     *
     * @param $sku_id
     * @return mixed 
     */
    public function getWareMainByID($ware_id,$page=1) {
        return $this->_client->api('360buy.ware.propimgs.search', array(
            'ware_id' => $ware_id,
            'page' => $page,
            'page_size' => 20,
            'fields' => 'img_url,color_id,is_main'
        ));
    }

    /**
     * 获取sku信息
     *
     * @param $sku_id
     * @return mixed jingdong.sku.read.findSkuById
     */
    
    public function findSkuById($sku_id) {
        return $this->_client->api('jingdong.sku.read.findSkuById', array(
            'skuId' => $sku_id,
            'field' => 'skuId,outerId,logo,jdPrice,skuName'
        ));
    }

}